package objects 
{
	/**
	 * ...
	 * @author Tuan
	 */
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import objects.cicleItem.cicleItemBartrack;
	import objects.MyObject;
	import starling.display.Image;
	import starling.display.MovieClip;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.filters.BlurFilter;
	import com.greensock.*; 
	import com.greensock.easing.*;
	import starling.textures.Texture;
	public class Bartrack extends MyObject
	{
		public var m_Sprite : Sprite;
		
		public var m_PointRally : Point;
		public var m_Move : Boolean;
		public var m_soliderManager : SoliderManager;
		
		public var m_circle : Image ;
		public var m_circleCollision : Image;
		public var m_Flag : Image;
		
		public var m_cicleItem:cicleItemBartrack;
		
		public var m_Road : Road;
		public var m_nodesRoad : Array;
		public var m_testChoose:Boolean;
		
		public var m_cointTotal:Number;
		public var m_range : Number;
		public var m_timeDoor : Number;
		private var m_rectTest : Rectangle;
		public var m_testBorn : Boolean;
		public var m_countBorn : Number;
	
		
		
		public function Bartrack( sprite : Sprite, _pos : Point, _road : Road,_coint:int) 
		{
			m_countBorn = 0;
			m_cicleItem = new cicleItemBartrack(sprite, _pos);
			m_nodesRoad = _road.getNodes();
			
			m_Sprite = sprite;
			m_pos =  _pos;
			m_id = Global.ID_TOWER_BARTRACK;
		
			
			sprite.addEventListener(TouchEvent.TOUCH, onTouch);
			this.addEventListener(Event.ADDED_TO_STAGE, init);
			
			m_Sprite.addChild(this);
			m_coolDown = 0;
			m_level = 1;
			setInfoFromCoint(_coint);
			m_cointTotal = _coint;
		}
		private function init(event :Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, init);
			m_testBorn = false;
			m_timeDoor = 0;
			m_movieVector = new Vector.<MovieClip>();
			m_animationState = Global.BARTRACK_OPEN;
			m_newAnimationState = Global.BARTRACK_OPEN;
			
		  	m_spriteMovie = new MovieClip(Assets.getSpriteSheet().getTextures("bartrack_open_lv_1"), 1);
			m_spriteMovie.pivotX = m_spriteMovie.width / 2;
			m_spriteMovie.pivotY = m_spriteMovie.height / 2;
			
			m_spriteMovie.scaleX = 0.4;
			m_spriteMovie.scaleY = 0.4;
			
			m_spriteMovie.x = m_pos.x; // nha` linh'
			m_spriteMovie.y = m_pos.y - 20;
			
			this.addChild(m_spriteMovie);
			
			m_circle =  new Image(Assets.getTexture("circle1")); // are movement of solider
			m_circle.alpha = 0.5;
			
			m_circle.pivotX = m_circle.width / 2;
			m_circle.pivotY =  m_circle.height / 2;
			
			m_circle.x = m_pos.x ;
			m_circle.y = m_pos.y ;
			
			this.addChild (m_circle);
			
			m_range = database.instance.RESULTSOLIDERARRAY[8];
			m_Flag = new Image (Assets.getTexture("RallyPoint"));
			m_Flag.alpha = 0;
			this.addChild(m_Flag);
			
			m_PointRally = new Point (m_pos.x , m_pos.y + 70);
			
			
			for (var i: int = 0; i < m_nodesRoad.length ; i ++)
			  {
			      if (m_nodesRoad[i].containsPoint ( m_PointRally) == true)
				  {
					  m_countBorn ++;
					   if (m_countBorn == 1)
					   {
						  m_PointRally = new Point (m_pos.x , m_pos.y - 80);
					   }

					 break;   
				  }
				    if ( i == m_nodesRoad.length -1)
					{
						    m_testBorn = true;
						
					}
			 }
			
			while (m_testBorn == false)
			{
			 for (i = 0; i < m_nodesRoad.length ; i ++)
			  {
			      if (m_nodesRoad[i].containsPoint ( m_PointRally) == true)
				  {
					  m_countBorn ++;
					 
					   if (m_countBorn == 2)
					   {
						   m_PointRally = new Point (m_pos.x -50, m_pos.y - 70);
					   }
					 break;   
				  }
				    if ( i == m_nodesRoad.length -1)
					{
						    m_testBorn = true;
							
					}
			 }
			}
			
		
			m_soliderManager = new SoliderManager(m_PointRally,m_Sprite, m_pos, m_level, this);
			m_soliderManager.addSolider ( new Solider (this, m_pos, new Point( m_PointRally.x + 30, m_PointRally.y)));			
			m_soliderManager.addSolider ( new Solider (this, m_pos, new Point( m_PointRally.x - 30, m_PointRally.y)));
			m_soliderManager.addSolider ( new Solider (this, m_pos, new Point( m_PointRally.x  + 2, m_PointRally.y + 30 )));
			
			//this.setAnchorPoint();
			
			
			m_Flag.pivotX = m_Flag.width / 2;
			m_Flag.pivotY = m_Flag.height / 2;
			
			m_Flag.x = m_pos.x + 60;
			m_Flag.y = m_pos.y + 55;	
			//this.setScale(m_range);
			
			m_circle.scaleX = m_range;
			m_circle.scaleY = m_range;
			//this.setPos(m_pos);
			
		}
		public function checkBorn( _m_PointRally : Point, _m_nodesRoad : Array) : void
		{
		
		}
		public function setAnchorPoint():void
		{
			m_circle.pivotX = m_circle.width / 2;
			m_circle.pivotY =  m_circle.height / 2;
			
			m_Flag.pivotX = m_Flag.width / 2;
			m_Flag.pivotY = m_Flag.height / 2;
			
			m_spriteMovie.pivotX = m_spriteMovie.width / 2;
			m_spriteMovie.pivotY = m_spriteMovie.height / 2;
			

		}
		
		public function setPos(_pos:Point):void
		{

			
			m_spriteMovie.x = m_pos.x; // nha` linh'
			m_spriteMovie.y = m_pos.y;
			
			m_circle.x = m_pos.x ;
			m_circle.y = m_pos.y ;
			
			m_Flag.x = m_pos.x + 60;
			m_Flag.y = m_pos.y + 55;	
		}
		
		public function setScale(_scale:Number):void
		{
			m_circle.scaleX = _scale;
			m_circle.scaleY = _scale;
					
		}
		override public function isTouch(_point:Point):void //touched end
		{

			
		}
		private function onTouch(e:TouchEvent):void 
		{
		if (database.instance.m_testTouch == true)
			{
			var touch:Touch = e.getTouch(stage);
			if(touch)
			{
				var _pointTouch:Point = new Point(touch.globalX, touch.globalY);
				if(touch.phase == TouchPhase.BEGAN)
				{
					
					if (m_cicleItem.m_result == 3)
				    {
					  if (TestCollisionelip_Point(_pointTouch, m_circle) == true )
					   {

						   for (var i: int = 0; i < m_nodesRoad.length ; i ++)
						   {
							   if (m_nodesRoad[i].containsPoint ( _pointTouch) == true)
							   {
								  
								  break;
								  
							   }
							   if ( i == m_nodesRoad.length -1)
							   {
							     m_soliderManager.setMoveByFlag(_pointTouch);
						         m_soliderManager.Move(_pointTouch);
								 m_PointRally = _pointTouch;
								 m_cicleItem.m_result = 0;
							   }							   
						   }
						 
					   }
					   else
						{ 
							m_testChoose == false;
						}
					  
				    }
					
					if (m_spriteMovie.bounds.containsPoint(_pointTouch) == true)
					{
						m_cicleItem.showCoint(m_cointTotal);
						m_cicleItem.clockAndUnclock();
						m_cicleItem.showCoint(m_cointTotal);
						if (m_testChoose == false)
						{
							m_testChoose = true;
							m_Sprite.setChildIndex(this, m_Sprite.numChildren);
							m_Sprite.setChildIndex(m_cicleItem, m_Sprite.numChildren);
						}
						else
						{
							m_testChoose = false;
						}
					}
					else
					{
						m_cicleItem.returnResult(_pointTouch);
						m_testChoose = false;
						if (m_cicleItem.m_result == -1)
						{
							m_testChoose = true;
						}
					}
				}
 
				else if(touch.phase == TouchPhase.ENDED)
				{
 
				}
 
				else if(touch.phase == TouchPhase.HOVER)
				{
					if (m_spriteMovie.bounds.containsPoint(_pointTouch) == true)
					{
						m_spriteMovie.filter= BlurFilter.createDropShadow(0,0,0xff0000,1,1,0.5);
						 
					}
					else
					{
						m_spriteMovie.filter= null;
					}
					
					m_cicleItem.blurFilter(_pointTouch);
				}
				
				
			}
			}
		}
		public function setInfoFromCoint(_coint:int):void
		{	
			m_cicleItem.setCicleInfo();
			m_cicleItem.showCoint(_coint);
		}
		public function levelUpAndSale(dt:Number, _coint:int):int
		{
			if (m_cicleItem.m_result == 1)
			{
				if ( m_cicleItem.m_baseItem[0].m_coint <= _coint)
				{
					//nang cap cac thong so
					//solider
					if (m_level < 3) //tam thoi chi cho nang 1 level duy nhat'
					  {
						m_soliderManager.m_level += 1;//nâng lv của solider
						if (m_soliderManager.m_listSolider.length == 0) // khi nang cap neu k du 3 linh thi cho hoi sinh het
						{
								m_soliderManager.addSolider ( new Solider (this, m_pos, new Point( m_PointRally.x + 30, m_PointRally.y)));	
								m_soliderManager.addSolider ( new Solider (this, m_pos, new Point( m_PointRally.x - 30, m_PointRally.y)));
								m_soliderManager.addSolider ( new Solider (this, m_pos, new Point( m_PointRally.x  + 2, m_PointRally.y + 30 )));
								this.m_animationState = Global.BARTRACK_OPEN;
						}
						else if (m_soliderManager.m_listSolider.length == 1)
						{
								m_soliderManager.addSolider ( new Solider (this, m_pos, new Point( m_PointRally.x - 30, m_PointRally.y)));
								m_soliderManager.addSolider ( new Solider (this, m_pos, new Point( m_PointRally.x  + 2, m_PointRally.y + 30 )));
								this.m_animationState = Global.BARTRACK_OPEN;
						}
						else if (m_soliderManager.m_listSolider.length == 2)
						{
								m_soliderManager.addSolider ( new Solider (this, m_pos, new Point( m_PointRally.x  + 2, m_PointRally.y + 30 )));
								this.m_animationState = Global.BARTRACK_OPEN;
						}
						
						m_soliderManager.updateLevel(m_soliderManager.m_level); // thay doi sprite, nang cap cac thuoc tinh'
					  }
					 
					m_level += 1;//nâng lv của trụ
					//cap nhat cac thong so
					_coint -= m_cicleItem.m_baseItem[0].m_coint;
					m_cicleItem.m_level = m_level;
					setInfoFromCoint(_coint);
					
					//tao lai hinh anh

					swapFrame (m_spriteMovie, Assets.getSpriteSheet().getTexture("bartrack_close_lv_"+m_level));
					
					
					m_spriteMovie.scaleX = 0.5;
					if (m_level == 3)
					{
						m_spriteMovie.scaleX = 0.6;
						m_spriteMovie.scaleY = 0.6;
					}

					
					//ket thuc
					m_cicleItem.m_result = 0;
					
					//tra ve coint
					return _coint;
				}
			}
			if (m_cicleItem.m_result == 2)
			{
				return _coint -= m_cicleItem.m_baseItem[1].m_coint;
			}
			return _coint;
		}
		override public function update(dt:Number):void
		{
			m_soliderManager.update(dt);
            if (m_animationState == Global.BARTRACK_OPEN)
			{
				swapFrame (m_spriteMovie, Assets.getSpriteSheet().getTexture("bartrack_open_lv_" + m_level));
				m_timeDoor += dt;
				if (m_timeDoor >= 1)
				{
					m_timeDoor = 0;
					swapFrame (m_spriteMovie, Assets.getSpriteSheet().getTexture("bartrack_close_lv_" + m_level));
					m_animationState =  Global.BARTRACK_CLOSE;
				}
			}

			
			if (m_testChoose == true)
			{
				m_cicleItem.alphaUp();
			}
			else
			{
				m_cicleItem.alphaDown();
				m_circle.alpha = 0;
			}
			
			  if (m_cicleItem.m_result == 3)
			  {
				  m_circle.alpha = 0.3;
			  }
		}
		public function swapFrame(clip:MovieClip, texture :Texture):void 
		{
	       clip.addFrame(texture);
		   clip.removeFrameAt(0);
		   clip.currentFrame = 0;
		}
	
		public function swapFrames(clip:MovieClip, textures:Vector.<Texture>):void 
		{
 
	      // remove all frame but one, since a MovieClip is not allowed to have 0 frames
		  
		  

          	while(clip.numFrames > 1){
		    clip.removeFrameAt(0);
	        }
 
	         // add new frames
	          for each (var texture:Texture in textures){
		       clip.addFrame(texture);
	          }
 
	        // remove that last previous frame
           	clip.removeFrameAt(0);
 
	         // set to frame 1
	        clip.currentFrame = 1;
		  }
		override public function Collision(_object:MyObject, _time:Number):void
		{
			m_soliderManager.Collison(_object, _time);
		}
		override public function release():void 
		{
			m_cicleItem.removeChildren();
			m_cicleItem.release();
			m_soliderManager.release();
			removeChildren();
			m_soliderManager = null;
		}
		
	}

}
